package com.mytime.database;

import java.util.HashSet;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.mytime.util.Functions;

@PersistenceCapable
public class Venue {

	@PrimaryKey
	@Persistent
	private Key key;

	@Persistent
	private String strKey;

	@Persistent
	private String encodedKey;

	@Persistent
	private String eventfulID;
	
	@Persistent
	private String name;
	
	@Persistent
	private String description;
	
	@Persistent
	private String address;
	
	@Persistent
	private String number;

	@Persistent
	private String complement;

	@Persistent
	private String region;

	@Persistent
	private String cityName;

	@Persistent
	private String cityId;
	
	@Persistent
	private String state;
				
	@Persistent
	private String postalCode;
	
	@Persistent
	private String country;
	
	@Persistent
	private String latitude;
	
	@Persistent
	private String longitude;
	
	@Persistent 
	private HashSet<Key> eventsList = new HashSet<Key>();

	//***************//
	// Constructors //
	//*************//
	public Venue() {
		
	}
	
	public Venue(JSONObject venue) {
		try {			
			if (venue.has("name")) {
				this.name = venue.getString("name");
				key = KeyFactory.createKey(Venue.class.getSimpleName(), name);
				encodedKey = KeyFactory.keyToString(key);
				strKey = Functions.removeSpecialCharacters(name).toLowerCase().replaceAll(" ", "_");
			}
			if (venue.has("description"))
				this.description = venue.getString("description");
			if (venue.has("address"))
				this.address = venue.getString("address");
			if (venue.has("number"))
				this.number = venue.getString("number");
			if (venue.has("complement"))
				this.complement = venue.getString("complement");
			if (venue.has("region"))
				this.region = venue.getString("region");
			if (venue.has("city")) {
				String cityName = venue.getString("city");
				this.cityName = cityName;
				this.cityId = Functions.removeSpecialCharacters(cityName).replaceAll(" ", "_").toLowerCase();
			} if (venue.has("state"))
				this.state = venue.getString("state");
			if (venue.has("postalCode"))
				this.postalCode = venue.getString("postalCode");
			if (venue.has("country"))
				this.country = venue.getString("country");
			if (venue.has("latitude"))
				this.latitude = venue.getString("latitude");
			if (venue.has("longitude"))
				this.longitude = venue.getString("longitude");

			
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	//***************//
	//Public Methods//
	//*************//
	
	public Key getKey() {
		return key;
	}

	public void setKey(Key id) {
		this.key = id;
	}

	public String getStrKey() {
		return strKey;
	}

	public void setStrKey(String strKey) {
		this.strKey = strKey;
	}

	public String getEncodedKey() {
		return encodedKey;
	}

	public void setEncodedKey(String encodedKey) {
		this.encodedKey = encodedKey;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getEventfulID() {
		return eventfulID;
	}

	public void setEventfulID(String eventfulID) {
		this.eventfulID = eventfulID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getComplement() {
		return complement;
	}

	public void setComplement(String complement) {
		this.complement = complement;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

	public String getCityId() {
		return cityId;
	}

	public void setCityId(String cityId) {
		this.cityId = cityId;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getPostalCode() {
		return postalCode;
	}

	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public boolean hasEvents() {
		return eventsList.size() > 0;
	}

	public HashSet<Key> getEventsList() {
		return eventsList;
	}

	public void setEventsList(HashSet<Key> eventsList) {
		this.eventsList = eventsList;
	}

	public void addEvent(Key eventId) {
		this.eventsList.add(eventId);
	}

	public void removeEvent(Key eventId) {
		this.eventsList.remove(eventId);
	}

	public void updateValues(JSONObject venue) {
		try {
			if (venue.has("name"))
				this.name = venue.getString("name");
			if (venue.has("description"))
				this.description = venue.getString("description");
			if (venue.has("address"))
				this.address = venue.getString("address");
			if (venue.has("number"))
				this.number = venue.getString("number");
			if (venue.has("complement"))
				this.complement = venue.getString("complement");
			if (venue.has("region"))
				this.region = venue.getString("region");
			if (venue.has("city")) {
				String cityName = venue.getString("city");
				this.cityName = cityName; 
				this.cityId = Functions.removeSpecialCharacters(cityName).replaceAll(" ", "_").toLowerCase();
			}
			if (venue.has("state"))
				this.state = venue.getString("state");
			if (venue.has("postalCode"))
				this.postalCode = venue.getString("postalCode");
			if (venue.has("country"))
				this.country = venue.getString("country");
			if (venue.has("latitude"))
				this.latitude = venue.getString("latitude");
			if (venue.has("longitude"))
				this.longitude = venue.getString("longitude");
			
		} catch (JSONException e) {
			e.printStackTrace();
		}		
	}
	
	public JSONObject toJSON() {
		JSONObject venue = new JSONObject();
		
		try {
			venue.put("key", strKey);
			addIfNotNull(venue, "venueName", this.name);
			addIfNotNull(venue, "venueDescription", this.description);
			addIfNotNull(venue, "address", this.address);
			addIfNotNull(venue, "number", this.number);
			addIfNotNull(venue, "complement", this.complement);
			addIfNotNull(venue, "region", this.region);
			addIfNotNull(venue, "city", this.cityName);
			addIfNotNull(venue, "state", this.state);
			addIfNotNull(venue, "postalCode", this.postalCode);
			addIfNotNull(venue, "country", this.country);
			addIfNotNull(venue, "latitude", this.latitude);
			addIfNotNull(venue, "longitude", this.longitude);
		
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return venue;
	}

	private void addIfNotNull(JSONObject j, String key, String value) throws JSONException {
		if (value != null && value.toString().length() != 0) {
			j.put(key, value);
		}
	}

}
